{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Relative Ranks"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #sorting #heap-priority-queue"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #排序 #堆（优先队列）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: findRelativeRanks"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #相对名次"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个长度为 <code>n</code> 的整数数组 <code>score</code> ，其中 <code>score[i]</code> 是第 <code>i</code> 位运动员在比赛中的得分。所有得分都 <strong>互不相同</strong> 。</p>\n",
    "\n",
    "<p>运动员将根据得分 <strong>决定名次</strong> ，其中名次第 <code>1</code> 的运动员得分最高，名次第 <code>2</code> 的运动员得分第 <code>2</code> 高，依此类推。运动员的名次决定了他们的获奖情况：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>名次第 <code>1</code> 的运动员获金牌 <code>\"Gold Medal\"</code> 。</li>\n",
    "\t<li>名次第 <code>2</code> 的运动员获银牌 <code>\"Silver Medal\"</code> 。</li>\n",
    "\t<li>名次第 <code>3</code> 的运动员获铜牌 <code>\"Bronze Medal\"</code> 。</li>\n",
    "\t<li>从名次第 <code>4</code> 到第 <code>n</code> 的运动员，只能获得他们的名次编号（即，名次第 <code>x</code> 的运动员获得编号 <code>\"x\"</code>）。</li>\n",
    "</ul>\n",
    "\n",
    "<p>使用长度为 <code>n</code> 的数组 <code>answer</code> 返回获奖，其中 <code>answer[i]</code> 是第 <code>i</code> 位运动员的获奖情况。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>score = [5,4,3,2,1]\n",
    "<strong>输出：</strong>[\"Gold Medal\",\"Silver Medal\",\"Bronze Medal\",\"4\",\"5\"]\n",
    "<strong>解释：</strong>名次为 [1<sup>st</sup>, 2<sup>nd</sup>, 3<sup>rd</sup>, 4<sup>th</sup>, 5<sup>th</sup>] 。</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>score = [10,3,8,9,4]\n",
    "<strong>输出：</strong>[\"Gold Medal\",\"5\",\"Bronze Medal\",\"Silver Medal\",\"4\"]\n",
    "<strong>解释：</strong>名次为 [1<sup>st</sup>, 5<sup>th</sup>, 3<sup>rd</sup>, 2<sup>nd</sup>, 4<sup>th</sup>] 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>n == score.length</code></li>\n",
    "\t<li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li>\n",
    "\t<li><code>0 &lt;= score[i] &lt;= 10<sup>6</sup></code></li>\n",
    "\t<li><code>score</code> 中的所有值 <strong>互不相同</strong></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [relative-ranks](https://leetcode.cn/problems/relative-ranks/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [relative-ranks](https://leetcode.cn/problems/relative-ranks/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[5,4,3,2,1]', '[10,3,8,9,4]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRelativeRanks(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: List[str]\n",
    "        \"\"\"\n",
    "        n1=sorted(nums,reverse=True)\n",
    "        nummap={}\n",
    "        for i in range(len(n1)):\n",
    "            if i==0:\n",
    "                nummap[n1[i]]=\"Gold Medal\"\n",
    "            elif i==1:\n",
    "                nummap[n1[i]]=\"Silver Medal\"\n",
    "            elif i==2:\n",
    "                nummap[n1[i]]=\"Bronze Medal\"\n",
    "            else:\n",
    "                nummap[n1[i]]=str(i+1)\n",
    "        result=[]\n",
    "        for j in nums:\n",
    "            result.append(nummap[j])\n",
    "        return result\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRelativeRanks(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: List[str]\n",
    "        \"\"\"\n",
    "        import copy\n",
    "        d=copy.copy(nums)\n",
    "        a=0\n",
    "        b=[0]*len(nums)\n",
    "        c=0\n",
    "        while a<len(d):\n",
    "            a+=1\n",
    "            c=d.index(max(nums))\n",
    "            nums.remove(max(nums))\n",
    "            if a == 1:\n",
    "                b[c]=\"Gold Medal\"\n",
    "                continue\n",
    "            if a == 2:\n",
    "                b[c]=\"Silver Medal\"\n",
    "                continue\n",
    "            if a == 3:\n",
    "                b[c]=\"Bronze Medal\"\n",
    "                continue\n",
    "            b[c]=str(a)\n",
    "        return b\n",
    "            \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRelativeRanks(self, nums: 'List[int]') -> 'List[str]':\n",
    "        '''\n",
    "        别人好的代码：\n",
    "        n=len(nums)\n",
    "        if n==1:\n",
    "            return [\"Gold Medal\"]\n",
    "        if n==2:\n",
    "            if nums[0]>nums[1]:\n",
    "                return [\"Gold Medal\",\"Silver Medal\"]\n",
    "            else:\n",
    "                return [\"Silver Medal\",\"Gold Medal\"]\n",
    "        temp=sorted(nums)\n",
    "        dic={temp[i]:str(n-i) for i in range(n)}\n",
    "        dic[temp[-1]]='Gold Medal'\n",
    "        dic[temp[-2]]='Silver Medal'\n",
    "        dic[temp[-3]]='Bronze Medal'\n",
    "        result=[dic[nums[i]] for i in range(n)]\n",
    "        return result\n",
    "        '''\n",
    "        # 效率太差\n",
    "        com_list = copy.deepcopy(nums)\n",
    "        res = 0\n",
    "        while res < len(com_list):\n",
    "            if len(com_list)-res == 1:\n",
    "                com_list[com_list.index(min(nums))] = 'Gold Medal'\n",
    "            elif len(com_list)-res == 2:\n",
    "                com_list[com_list.index(min(nums))] = 'Silver Medal'\n",
    "            elif len(com_list)-res == 3:\n",
    "                com_list[com_list.index(min(nums))] = 'Bronze Medal'\n",
    "            else:\n",
    "                 com_list[com_list.index(min(nums))] = str(len(com_list)-res)\n",
    "            nums.remove(min(nums))\n",
    "            res += 1\n",
    "        return com_list\n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRelativeRanks(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: List[str]\n",
    "        \"\"\"\n",
    "        position = []\n",
    "        result = []\n",
    "        for i in range(len(nums)):\n",
    "            position.append(i)\n",
    "            result.append(\"\")\n",
    "        for i in range(len(nums)):\n",
    "            for j in range(i+1, len(nums)):\n",
    "                if nums[i] < nums[j]:\n",
    "                    nums[i], nums[j] = nums[j], nums[i]\n",
    "                    position[i], position[j] = position[j], position[i]\n",
    "        for i in range(len(position)):\n",
    "            if i == 0:\n",
    "                result[position[i]] = \"Gold Medal\"\n",
    "            elif i == 1:\n",
    "                result[position[i]] = \"Silver Medal\"\n",
    "            elif i == 2:\n",
    "                result[position[i]] = \"Bronze Medal\"\n",
    "            else:\n",
    "                result[position[i]] = str(i+1)\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRelativeRanks(self, scores):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: List[str]\n",
    "        \"\"\"\n",
    "        medals = [\"Gold Medal\", \"Silver Medal\", \"Bronze Medal\"]\n",
    "        sorted_scores_dict = {}\n",
    "        for order,score in enumerate(sorted(scores, reverse = True)):\n",
    "            if order <= 2:\n",
    "                sorted_scores_dict[score] = medals[order]\n",
    "            else:\n",
    "                sorted_scores_dict[score] = str(order + 1)\n",
    "        res = [sorted_scores_dict[score] for score in scores]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRelativeRanks(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: List[str]\n",
    "        \"\"\"\n",
    "        arr = sorted([[-n, i] for i, n in enumerate(nums)])\n",
    "        res = [''] * len(nums)\n",
    "        for j, p in enumerate(arr):\n",
    "            if j == 0:\n",
    "                res[p[1]] = 'Gold Medal'\n",
    "            elif j == 1:\n",
    "                res[p[1]] = 'Silver Medal'\n",
    "            elif j == 2:\n",
    "                res[p[1]] = 'Bronze Medal'\n",
    "            else:\n",
    "                res[p[1]] = str(j + 1)\n",
    "        return res\n",
    "                \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRelativeRanks(self, nums: 'List[int]') -> 'List[str]':\n",
    "        sortNums=sorted(nums,reverse=True)\n",
    "        maps={}\n",
    "        for i in range(len(sortNums)):\n",
    "            maps[sortNums[i]]=i+1\n",
    "        \n",
    "        for i in range(len(nums)):\n",
    "            if maps[nums[i]]==1:\n",
    "                nums[i]='Gold Medal'\n",
    "            elif maps[nums[i]]==2:\n",
    "                nums[i]='Silver Medal'\n",
    "            elif maps[nums[i]]==3:\n",
    "                nums[i]='Bronze Medal'\n",
    "            else:\n",
    "                nums[i]=str(maps[nums[i]])\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRelativeRanks(self, nums: List[int]) -> List[str]:\n",
    "        tmp = [x for x in nums]\n",
    "        tmp.sort(reverse=True) \n",
    "        dic={}\n",
    "\n",
    "        for r in range(len(tmp)):\n",
    "            if r==0:\n",
    "                dic[tmp[r]]=\"Gold Medal\"\n",
    "            elif r==1:\n",
    "                dic[tmp[r]]=\"Silver Medal\"\n",
    "            elif r==2:\n",
    "                dic[tmp[r]]=\"Bronze Medal\"\n",
    "            else:\n",
    "                dic[tmp[r]]=str(r+1)\n",
    "\n",
    "        return [dic[x] for x in nums]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRelativeRanks(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: List[str]\n",
    "        \"\"\"\n",
    "        if len(nums) == 1:\n",
    "            return ['Gold Medal']\n",
    "        if len(nums) == 2:\n",
    "            if nums[0]>nums[1]:\n",
    "                return ['Gold Medal', 'Silver Medal']\n",
    "            else:\n",
    "                return ['Silver Medal', 'Gold Medal']\n",
    "        else:\n",
    "            res = ['Gold Medal', 'Silver Medal', 'Bronze Medal']\n",
    "            for i in range(4,len(nums)+1):\n",
    "                res.append(str(i))\n",
    "            orders = list(range(len(nums)))\n",
    "            res_dict = dict(zip(nums, orders))\n",
    "            nums.sort(reverse=True)\n",
    "            orders = []\n",
    "            for key in nums:\n",
    "                orders.append(res_dict[key])\n",
    "            res_dict.clear()\n",
    "            res_dict = dict(zip(orders, res))\n",
    "            orders.sort()\n",
    "            res = []\n",
    "            for key in orders:\n",
    "                res.append(res_dict[key])\n",
    "            return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRelativeRanks(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: List[str]\n",
    "        \"\"\"\n",
    "        sortNums=sorted(nums,reverse=True)\n",
    "        maps={}\n",
    "        for i in range(len(sortNums)):\n",
    "            maps[sortNums[i]]=i+1\n",
    "        \n",
    "        for i in range(len(nums)):\n",
    "            if maps[nums[i]]==1:\n",
    "                nums[i]='Gold Medal'\n",
    "            elif maps[nums[i]]==2:\n",
    "                nums[i]='Silver Medal'\n",
    "            elif maps[nums[i]]==3:\n",
    "                nums[i]='Bronze Medal'\n",
    "            else:\n",
    "                nums[i]=str(maps[nums[i]])\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def findRelativeRanks(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: List[str]\n",
    "        \"\"\"\n",
    "        atheles = map(lambda score: {\"score\": score}, nums)\n",
    "        atheles = sorted(atheles, key=lambda a: - a[\"score\"])\n",
    "        top3Ranks = [\"Gold Medal\", \"Silver Medal\", \"Bronze Medal\"]\n",
    "        ranks = {}\n",
    "        for i in range(len(atheles)):\n",
    "            if i < 3:\n",
    "                rank = top3Ranks[i]\n",
    "            else:\n",
    "                rank = str(i+1)\n",
    "            ranks[atheles[i][\"score\"]] = rank\n",
    "\n",
    "        return list(map(lambda score: ranks[score], nums))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRelativeRanks(self, nums: List[int]) -> List[str]:\n",
    "        new_nums= sorted(nums,reverse = True)\n",
    "        m = {1:\"Gold Medal\",2:\"Silver Medal\",3:\"Bronze Medal\"}\n",
    "        r_nums = []\n",
    "        for i in nums:\n",
    "            x = new_nums.index(i)+1\n",
    "            if x in m.keys():\n",
    "                r_nums.append(m[x])\n",
    "            else:\n",
    "                r_nums.append(str(x))\n",
    "        return r_nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRelativeRanks(self, nums: List[int]) -> List[str]:\n",
    "        sortnum = sorted(nums, reverse=True)\n",
    "        res = []\n",
    "        for i in nums:\n",
    "            if sortnum.index(i)==0:\n",
    "                res.append(\"Gold Medal\")\n",
    "            elif sortnum.index(i)==1:\n",
    "                res.append(\"Silver Medal\")\n",
    "            elif sortnum.index(i)==2:\n",
    "                res.append(\"Bronze Medal\")\n",
    "            else:\n",
    "                res.append(str(sortnum.index(i)+1))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRelativeRanks(self, nums: List[int]) -> List[str]:\n",
    "        tmp = nums.copy()\n",
    "        def mysort(nums):\n",
    "            if len(nums) < 2:\n",
    "                return nums\n",
    "            std = nums[0]\n",
    "            i, j = 1, len(nums)-1\n",
    "            while i < j:\n",
    "                while i<j and nums[i] >= std:\n",
    "                    i += 1\n",
    "                while i<j and nums[j] < std:\n",
    "                    j -= 1\n",
    "                nums[i], nums[j] = nums[j], nums[i]\n",
    "            if nums[i] >= std:\n",
    "                i += 1\n",
    "\n",
    "            return mysort(nums[1:i]) + [std] + mysort(nums[i:])\n",
    "\n",
    "        nums = mysort(nums)\n",
    "        \n",
    "        l = len(nums)\n",
    "        for i in range(l):\n",
    "            if tmp[i] == nums[0]:\n",
    "                tmp[i] = 'Gold Medal'\n",
    "            elif tmp[i] == nums[1]:\n",
    "                tmp[i] = 'Silver Medal'\n",
    "            elif tmp[i] == nums[2]:\n",
    "                tmp[i] = 'Bronze Medal'\n",
    "            else:\n",
    "                tmp[i] = str(nums.index(tmp[i]) + 1)\n",
    "\n",
    "        return tmp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRelativeRanks(self, nums: List[int]) -> List[str]:\n",
    "        nums_copy=sorted(nums)[::-1]\n",
    "        hashmap={}\n",
    "        for i,num in enumerate(nums_copy):\n",
    "            if i==0:\n",
    "                hashmap[num]='Gold Medal'\n",
    "            elif i==1:\n",
    "                hashmap[num]='Silver Medal'\n",
    "            elif i==2:\n",
    "                hashmap[num]='Bronze Medal'\n",
    "            else:\n",
    "                hashmap[num]=str(i+1)\n",
    "        return [hashmap[num] for num in nums]\n",
    "\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRelativeRanks(self, nums: List[int]) -> List[str]:\n",
    "        p=nums.copy()\n",
    "        l=len(nums)\n",
    "        p.sort()\n",
    "        re=[]\n",
    "        for i in range(l):\n",
    "            j=p.index(nums[i])\n",
    "            if j==l-1:\n",
    "                re.append(\"Gold Medal\")\n",
    "            elif j==l-2:\n",
    "                re.append(\"Silver Medal\")\n",
    "            elif j==l-3:\n",
    "                re.append(\"Bronze Medal\")\n",
    "            else:\n",
    "                re.append(str(l-j))\n",
    "        return re\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRelativeRanks(self, nums: List[int]) -> List[str]:\n",
    "        dummy=nums\n",
    "\n",
    "        nums=sorted(nums)\n",
    "        nums0=nums[::-1]\n",
    "        # nums[0]=\"Gold Medal\"\n",
    "        # nums[1]=\"Silver Medal\"\n",
    "        # nums[2]=\"Bronze Medal\"\n",
    "\n",
    "        # nums=map(nums.index,dummy)\n",
    "        nums=map(lambda x:nums0.index(x)+1,dummy)\n",
    "        return map(lambda x:{1:\"Gold Medal\",2:'Silver Medal',3:\"Bronze Medal\"}.get(x,str(x)),nums)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRelativeRanks(self, nums):\n",
    "        j = len(nums)\n",
    "        for i in sorted(nums):\n",
    "            nums[nums.index(i)] = str(j)\n",
    "            j -= 1\n",
    "        for i, j in enumerate(nums):\n",
    "            if j == '1':\n",
    "                nums[i] = \"Gold Medal\"\n",
    "            elif j == '3':\n",
    "                nums[i] = \"Bronze Medal\"\n",
    "            elif j == '2':\n",
    "                nums[i] = \"Silver Medal\"\n",
    "        return nums\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRelativeRanks(self, nums: List[int]) -> List[str]:\n",
    "        a = nums.copy()\n",
    "        a.sort()\n",
    "        for i in range(len(a)):\n",
    "            if i == 0:\n",
    "                nums[nums.index(a[-i-1])] = 'Gold Medal'\n",
    "            elif i == 1:\n",
    "                nums[nums.index(a[-i-1])] = 'Silver Medal'\n",
    "            elif i == 2:\n",
    "                nums[nums.index(a[-i-1])] = 'Bronze Medal'\n",
    "            else:\n",
    "                nums[nums.index(a[-i-1])] = str(i+1)\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRelativeRanks(self, nums: List[int]) -> List[str]:\n",
    "        sort_nums=sorted(nums)\n",
    "        d={}\n",
    "        N=len(sort_nums)\n",
    "        for i in range(N):\n",
    "            if sort_nums[i] not in d:\n",
    "                d[sort_nums[i]]=N-i\n",
    "        for j in range(N):\n",
    "            if nums[j] in d:\n",
    "                if d[nums[j]]==1:\n",
    "                    nums[j]=\"Gold Medal\"\n",
    "                elif d[nums[j]]==2:\n",
    "                    nums[j]=\"Silver Medal\"\n",
    "                elif d[nums[j]]==3:\n",
    "                    nums[j]=\"Bronze Medal\"\n",
    "                else:\n",
    "                    nums[j]=str(d[nums[j]])\n",
    "        return nums "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRelativeRanks(self, nums: List[int]) -> List[str]:\n",
    "        dict = {}\n",
    "        #分数存的是下标\n",
    "        for i in range(len(nums)):\n",
    "            dict[nums[i]] = i\n",
    "        nums.sort()\n",
    "        nums.reverse()\n",
    "        ans = [0] * (len(nums))\n",
    "        for i in range(len(nums)):\n",
    "            if i == 0:\n",
    "                ans[dict[nums[i]]] = \"Gold Medal\"\n",
    "            elif i == 1:\n",
    "                ans[dict[nums[i]]] = \"Silver Medal\"\n",
    "            elif i == 2:\n",
    "                ans[dict[nums[i]]] = \"Bronze Medal\"\n",
    "            else:\n",
    "                ans[dict[nums[i]]] = str(i + 1)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRelativeRanks(self, nums: List[int]) -> List[str]:\n",
    "        if len(nums)<0:\n",
    "            return []\n",
    "        pairs = []\n",
    "        for i in range(len(nums)):\n",
    "            pairs.append([i,nums[i]])\n",
    "        pairs = sorted(pairs, key=lambda x:x[1], reverse = True)\n",
    "        re = [0] * len(nums)\n",
    "        for i in range(len(nums)):\n",
    "            if i > 2:\n",
    "                re[pairs[i][0]] = str(i+1)\n",
    "            elif i == 0:\n",
    "                re[pairs[i][0]] = 'Gold Medal'\n",
    "            elif i == 1:\n",
    "                re[pairs[i][0]] = 'Silver Medal'\n",
    "            else:\n",
    "                re[pairs[i][0]] = 'Bronze Medal'\n",
    "        return re"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRelativeRanks(self, score: List[int]) -> List[str]:\n",
    "        ini=sorted(score)\n",
    "        ini.reverse()\n",
    "\n",
    "        for i in range(len(score)):\n",
    "            if score[i]==ini[0]:\n",
    "                score[i]='Gold Medal'\n",
    "            elif score[i]==ini[1]:\n",
    "                score[i]='Silver Medal'\n",
    "            elif score[i]==ini[2]:\n",
    "                score[i]='Bronze Medal'\n",
    "            else:\n",
    "                score[i]=str(ini.index(score[i])+1)\n",
    "        return score\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRelativeRanks(self, score: List[int]) -> List[str]:\n",
    "        score0=score.copy()\n",
    "        score.sort(reverse=True)\n",
    "        a=[]\n",
    "        for i in score0:\n",
    "            \n",
    "            if (score.index(i)+1==1):\n",
    "                a.append('Gold Medal')\n",
    "            elif (score.index(i)+1==2):\n",
    "                a.append('Silver Medal')\n",
    "            elif (score.index(i)+1==3):\n",
    "                a.append('Bronze Medal')\n",
    "            else:\n",
    "                a.append(str(score.index(i)+1))\n",
    "            \n",
    "        \n",
    "        return a\n",
    "\n",
    "\n",
    "        \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRelativeRanks(self, score: List[int]) -> List[str]:\n",
    "        sort1 = sorted(score,reverse=True)\n",
    "        for i in sort1:\n",
    "            index1=score.index(i)\n",
    "            if sort1.index(i)==0:\n",
    "                score[index1]='Gold Medal'\n",
    "            elif sort1.index(i)==1:\n",
    "                score[index1]='Silver Medal'\n",
    "            elif sort1.index(i)==2:\n",
    "                score[index1]='Bronze Medal'\n",
    "            else:\n",
    "                score[index1]='%d'%(sort1.index(i) + 1)\n",
    "        return score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRelativeRanks(self, score: List[int]) -> List[str]:\n",
    "        n = len(score)\n",
    "        scorel = sorted(score,reverse=True)\n",
    "        ll = []\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                if score[i] == scorel[j]:\n",
    "                    ll.append(str(j+1))\n",
    "        for i in range(len(ll)):\n",
    "            if ll[i] == \"1\":\n",
    "                ll[i] = 'Gold Medal'\n",
    "            if ll[i] == \"2\":\n",
    "                ll[i] = 'Silver Medal'\n",
    "            if ll[i] == \"3\":\n",
    "                ll[i] = 'Bronze Medal'\n",
    "        return ll\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRelativeRanks(self, score: List[int]) -> List[str]:\n",
    "        ans=[]\n",
    "        ft=['Gold Medal','Silver Medal','Bronze Medal']\n",
    "        sortlist=sorted(score,reverse=True)\n",
    "        for i in score:\n",
    "            if sortlist.index(i)<3:\n",
    "                ans.append(ft[sortlist.index(i)])\n",
    "            else:\n",
    "                ans.append(str(sortlist.index(i)+1))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRelativeRanks(self, score: List[int]) -> List[str]:\n",
    "        dict={}\n",
    "        for i in range(len(score)): \n",
    "            dict[score[i]]=i\n",
    "        score.sort(reverse=True)\n",
    "        n=len(score)\n",
    "        ans=['']*n\n",
    "        if n>=1:ans[dict[score[0]]]='Gold Medal'\n",
    "        if n>=2:ans[dict[score[1]]]='Silver Medal'\n",
    "        if n>=3:ans[dict[score[2]]]='Bronze Medal'\n",
    "        if n>4:\n",
    "            for i in range(3,n):\n",
    "                ans[dict[score[i]]]=str(i+1)\n",
    "        return ans\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRelativeRanks(self, score: List[int]) -> List[str]:\n",
    "        res = [None for _ in range(len(score))]\n",
    "        for prize, oridata in enumerate(sorted(enumerate(score), key=lambda x: -x[1])):\n",
    "            idx, _ = oridata\n",
    "            if prize == 0:\n",
    "                res[idx] = \"Gold Medal\"\n",
    "            elif prize == 1:\n",
    "                res[idx] = \"Silver Medal\"\n",
    "            elif prize == 2:\n",
    "                res[idx] = \"Bronze Medal\"\n",
    "            else:\n",
    "                res[idx] = str(prize+1)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRelativeRanks(self, score: List[int]) -> List[str]:\n",
    "        tmp = sorted(score)[::-1]\n",
    "        res = []\n",
    "        for i in score:\n",
    "            if tmp.index(i) == 0:\n",
    "                res.append(\"Gold Medal\")\n",
    "            elif tmp.index(i) == 1:\n",
    "                res.append(\"Silver Medal\")\n",
    "            elif tmp.index(i) == 2:\n",
    "                res.append(\"Bronze Medal\")\n",
    "            else:\n",
    "                res.append(str(tmp.index(i) + 1))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRelativeRanks(self, score: List[int]) -> List[str]:\n",
    "        a=sorted(score,reverse=True)\n",
    "        len_=len(score)\n",
    "        list_=[0 for _ in range(len_)]\n",
    "        if len_>=3:\n",
    "            mingci1,mingci2,mingci3=a[0],a[1],a[2]\n",
    "        elif len_==1:\n",
    "            mingci1=a[0]\n",
    "        else:\n",
    "            mingci1,mingci2=a[0],a[1]\n",
    "        for i in range(len_):\n",
    "            if score[i]==mingci1:\n",
    "                list_[i]=\"Gold Medal\"\n",
    "            elif score[i]==mingci2:\n",
    "                list_[i]=\"Silver Medal\"\n",
    "            elif score[i]==mingci3:\n",
    "                list_[i]=\"Bronze Medal\"\n",
    "            else:\n",
    "                list_[i]=str(a.index(score[i])+1)\n",
    "        return list_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    ## 思路：从大到小排序，排序过程中，索引值一起跟着变动。然后对新列表按照相应的索引位置进行赋值\n",
    "    def findRelativeRanks(self, score: List[int]) -> List[str]:\n",
    "        if len(score) == 1: return ['Gold Medal']\n",
    "        if len(score) == 2: return ['Gold Medal','Silver Medal'] if score[0]>score[1] else ['Silver Medal','Gold Medal']\n",
    "        # 这里使用选择排序来实现排序（将所有元素与第一个元素比较，将更小（大）的元素换到第一个位置；在未排序的区域依次操作）\n",
    "        # 缺点是，选择排序太耗时了；冒泡排序也好不到哪去，都是O(n^2)的时间复杂度。两者唯一优点是空间复杂度低。\n",
    "        n = len(score)\n",
    "        idx = [i for i in range(n)]\n",
    "        '''\n",
    "        for j in range(n-1):\n",
    "            for i in range(j,n):\n",
    "                if score[i]>score[j]:\n",
    "                    score[i],score[j]=score[j],score[i]\n",
    "                    idx[i],idx[j]=idx[j],idx[i]\n",
    "        '''\n",
    "        # 冒泡排序\n",
    "        for j in range(n-1, 0, -1):\n",
    "            for i in range(0,j):\n",
    "                if score[i]<score[i+1]:\n",
    "                    score[i], score[i+1] = score[i+1], score[i]\n",
    "                    idx[i], idx[i+1] = idx[i+1], idx[i]\n",
    "        score[idx[0]] = 'Gold Medal'\n",
    "        score[idx[1]] = 'Silver Medal'\n",
    "        score[idx[2]] = 'Bronze Medal'\n",
    "        for i in range(3,n):\n",
    "            score[idx[i]] = str(i+1)\n",
    "        return score\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findRelativeRanks(self, score: List[int]) -> List[str]:\n",
    "        s=sorted(score,reverse=True)\n",
    "        a=tuple(s)\n",
    "        c=dict.fromkeys(tuple(score))\n",
    "        b=dict.fromkeys(a)\n",
    "        for i in range(len(s)):\n",
    "            if i == 0:\n",
    "                b[s[i]] = 'Gold Medal'\n",
    "            if i == 1:\n",
    "                b[s[i]] = 'Silver Medal'\n",
    "            if i == 2:\n",
    "                b[s[i]] = 'Bronze Medal'\n",
    "            if i>2:\n",
    "                b[s[i]] = str(i+1)\n",
    "        for k in c:\n",
    "            c[k]=b[k]\n",
    "        return list(c.values())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# 总结：\n",
    "# 学到的东西\n",
    "# dic=dict(zip(list1,list2))\n",
    "# dic['key']=values\n",
    "class Solution:\n",
    "    def findRelativeRanks(self, score: List[int]) -> List[str]:\n",
    "        score_sorted=sorted(score,reverse=True)\n",
    "        index=[x for x in range(1,len(score)+1)]\n",
    "        # print(score_sorted)\n",
    "        # print(index)\n",
    "        ranking_dic=dict(zip(score_sorted,index))\n",
    "        # print(ranking_dic)\n",
    "        result=['']*len(score)\n",
    "        for i in range(len(score)):\n",
    "            if ranking_dic[score[i]]==1:\n",
    "                result[i]+='Gold Medal'\n",
    "            elif ranking_dic[score[i]]==2:\n",
    "                result[i]+='Silver Medal'\n",
    "            elif ranking_dic[score[i]]==3:\n",
    "                result[i]+='Bronze Medal'\n",
    "            else:\n",
    "                result[i]+=str(ranking_dic[score[i]])\n",
    "        return result"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
